home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2001 September / PC-WELT 9-2001.ISO / software / hw / brennen / flask_src.exe / Audio / AC3 / AC3Dec / decode.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-06  |  3.6 KB  |  150 lines

  1. /* 
  2.  *    decode.c
  3.  *
  4.  *    Copyright (C) Aaron Holtzman - May 1999
  5.  *
  6.  *  This file is part of ac3dec, a free Dolby AC-3 stream decoder.
  7.  *    
  8.  *  ac3dec is free software; you can redistribute it and/or modify
  9.  *  it under the terms of the GNU General Public License as published by
  10.  *  the Free Software Foundation; either version 2, or (at your option)
  11.  *  any later version.
  12.  *   
  13.  *  ac3dec is distributed in the hope that it will be useful,
  14.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16.  *  GNU General Public License for more details.
  17.  *   
  18.  *  You should have received a copy of the GNU General Public License
  19.  *  along with GNU Make; see the file COPYING.  If not, write to
  20.  *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. 
  21.  *
  22.  *
  23.  */
  24.  
  25. #ifdef HAVE_CONFIG_H
  26. #include "config.h"
  27. #endif 
  28.  
  29. #include <stdlib.h>
  30. #include <stdio.h>
  31. #include <errno.h>
  32. #include <string.h>
  33. //#include <sys/time.h>
  34. #include "ac3.h"
  35. #include "ac3_internal.h"
  36. #include "bitstream.h"
  37. #include "imdct.h"
  38. #include "exponent.h"
  39. #include "coeff.h"
  40. #include "bit_allocate.h"
  41. #include "parse.h"
  42. #include "crc.h"
  43. #include "stats.h"
  44. #include "rematrix.h"
  45. #include "sanity_check.h"
  46. #include "downmix.h"
  47. #include "debug.h"
  48.  
  49. //our global config structure
  50. ac3_config_t ac3_config;
  51. uint_32 error_flag = 0;
  52.  
  53. static audblk_t audblk;
  54. static bsi_t bsi;
  55. static syncinfo_t syncinfo;
  56. static uint_32 frame_count = 0;
  57. static uint_32 done_banner;
  58. static ac3_frame_t frame;
  59.  
  60. //the floating point samples for one audblk
  61. static stream_samples_t samples;
  62.  
  63. //the integer samples for the entire frame (with enough space for 2 ch out)
  64. //if this size change, be sure to change the size when muting
  65. static sint_16 s16_samples[2 * 6 * 256];
  66.  
  67. void
  68. ac3_init(ac3_config_t *config)
  69. {
  70.     memcpy(&ac3_config,config,sizeof(ac3_config_t));
  71.  
  72.     bitstream_init(config->fill_buffer_callback);
  73.     imdct_init();
  74.     sanity_check_init(&syncinfo,&bsi,&audblk);
  75.  
  76.     frame.audio_data = s16_samples;
  77. }
  78.  
  79. ac3_frame_t*
  80. ac3_decode_frame(void)
  81. {
  82.     uint_32 i;
  83.  
  84.     //find a syncframe and parse
  85.     parse_syncinfo(&syncinfo);
  86.     if(error_flag)
  87.         goto error;
  88.  
  89.     dprintf("(decode) begin frame %d\n",frame_count++);
  90.     frame.sampling_rate = syncinfo.sampling_rate;
  91.  
  92.     parse_bsi(&bsi);
  93.  
  94.     if(!done_banner)
  95.     {
  96.         stats_print_banner(&syncinfo,&bsi);
  97.         done_banner = 1;
  98.     }
  99.  
  100.     for(i=0; i < 6; i++)
  101.     {
  102.         //Initialize freq/time sample storage
  103.         memset(samples,0,sizeof(float) * 256 * (bsi.nfchans + bsi.lfeon));
  104.  
  105.         // Extract most of the audblk info from the bitstream
  106.         // (minus the mantissas 
  107.         parse_audblk(&bsi,&audblk);
  108.  
  109.         // Take the differential exponent data and turn it into
  110.         // absolute exponents 
  111.         exponent_unpack(&bsi,&audblk); 
  112.         if(error_flag)
  113.             goto error;
  114.  
  115.         // Figure out how many bits per mantissa 
  116.         bit_allocate(syncinfo.fscod,&bsi,&audblk);
  117.  
  118.         // Extract the mantissas from the stream and
  119.         // generate floating point frequency coefficients
  120.         coeff_unpack(&bsi,&audblk,samples);
  121.         if(error_flag)
  122.             goto error;
  123.  
  124.         if(bsi.acmod == 0x2)
  125.             rematrix(&audblk,samples);
  126.  
  127.         // Convert the frequency samples into time samples 
  128.         imdct(&bsi,&audblk,samples);
  129.  
  130.         // Downmix into the requested number of channels
  131.         // and convert floating point to sint_16
  132.         downmix(&bsi,samples,&s16_samples[i * 2 * 256]);
  133.  
  134.         sanity_check(&syncinfo,&bsi,&audblk);
  135.         if(error_flag)
  136.             goto error;
  137.     }
  138.     parse_auxdata(&syncinfo);
  139.  
  140.     return &frame;    
  141.  
  142. error:
  143.     //mute the frame
  144.     memset(s16_samples,0,sizeof(sint_16) * 256 * 2 * 6);
  145.  
  146.     error_flag = 0;
  147.     return &frame;    
  148.  
  149. }
  150.